package com.intelligence.farm.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligence.entity.model.FarmModel;
import com.intelligence.entity.pojo.City;
import com.intelligence.entity.pojo.County;
import com.intelligence.entity.pojo.Farm;
import com.intelligence.entity.pojo.Province;
import com.intelligence.entity.result.Result;
import com.intelligence.entity.result.ResultCodeEnum;
import com.intelligence.entity.vo.FarmVo;
import com.intelligence.farm.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author : RulerWood
 * @Data : 2024/5/16 - 18:48
 */
@Slf4j
@RestController
@RequestMapping("/system/farm")
public class FarmController {

    @Autowired
    private FarmService farmService;
    @Autowired
    private FarmTypeService farmTypeService;
    @Autowired
    private ProvinceService provinceService;   //省
    @Autowired
    private CityService cityService;           //市
    @Autowired
    private CountyService countyService;       //区

    /**
     * 查询所有基地
     *
     * @param offset   分页起始位置
     * @param pageSize 分页每页显示条目数
     * @param cityName 根据城市查询
     * @return
     */
    @GetMapping("/farmList")
    public Result<Page<FarmVo>> farmList(@RequestParam(value = "offset") Integer offset,
                                         @RequestParam(value = "pageSize") Integer pageSize,
                                         String cityName, String type) {
        log.info("cityName------list--->{}", cityName);
        log.info("type------list--->{}", type);
        log.info("offset------list--->{}", offset);
        log.info("pageSize------list--->{}", pageSize);
//        QueryWrapper<Farm> farmQueryWrapper = new QueryWrapper<>();
//        if (cityName != null && !cityName.equals("")) {
//            farmQueryWrapper.eq("province", cityName);
//        }
//        if (type != null && !type.equals("")) {
//            farmQueryWrapper.eq("type", type);
//        }
        return Result.build(farmService.findFarmAndType(offset, pageSize, cityName, type), ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据登录的用户id找寻当前用户的登录下的所有基地信息(只有基地管理员登录时，才会使用到这个基地表)
     *
     * @param userId
     * @return
     */
    @GetMapping("/getFarmIdFromUserId/{userId}")
    public List<Farm> getFarmIdFromUserId(@PathVariable("userId") Integer userId) {
        System.out.println("userId------getFarmIdFromUserId--->{}" + userId);
        QueryWrapper<Farm> farmQueryWrapper = new QueryWrapper<>();
        farmQueryWrapper.eq("userId", userId);
        return farmService.list(farmQueryWrapper);
    }

    /**
     * 查询全部的基地类型
     *
     * @return
     */
    @GetMapping("/allFarmTypes")
    public Result<List<String>> allFarmTypes() {
        return Result.build(this.farmTypeService.list(), ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询所有省份
     *
     * @return
     */
    @GetMapping("/provinceList")
    public Result<List<Province>> province() {
        return Result.build(provinceService.list(), ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询市区
     *
     * @param provinceName 那个省份的城市
     * @return
     */
    @GetMapping("/cityList")
    public Result<List<City>> cityList(String provinceName) {
        log.info("provinceName------cityList--->{}", provinceName);
        Province province = this.provinceService.getOptById(provinceName).get();
        QueryWrapper<City> cityQueryWrapper = new QueryWrapper<>();
        if (provinceName != null && !provinceName.equals("")) {
            cityQueryWrapper.eq("provinceCode", province.getCode());
        }
        return Result.build(cityService.list(cityQueryWrapper), ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询区县
     *
     * @param cityName 那个城市的区县
     * @return
     */
    @GetMapping("/countyList")
    public Result<List<County>> countyList(String cityName) {
        log.info("cityName------countyList--->{}", cityName);
        City city = this.cityService.getOptById(cityName).get();
        QueryWrapper<County> countyQueryWrapper = new QueryWrapper<>();
        if (cityName != null && !cityName.equals("")) {
            countyQueryWrapper.eq("cityCode", city.getCode());
        }
        return Result.build(countyService.list(countyQueryWrapper), ResultCodeEnum.SUCCESS);
    }

    /**
     * 添加基地
     *
     * @param farmModel 基地信息
     * @return
     */
    @PostMapping("/saveFarm")
    public Result saveFarm(@RequestBody FarmModel farmModel) {
        log.info("farmModel------saveFarm--->{}", farmModel);
        Farm farm = new Farm();
        BeanUtil.copyProperties(farmModel, farm);
        Province province = this.provinceService.getOptById(farmModel.getProvinceCode()).get();
        farm.setProvince(province.getName());
        City city = this.cityService.getOptById(farmModel.getCityCode()).get();
        farm.setCity(city.getName());
        County county = this.countyService.getOptById(farmModel.getCountyCode()).get();
        farm.setDistrict(county.getName());
        log.info("copy之后的farm对象是------>{}", farm);
        farmService.save(farm);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改基地
     *
     * @param farmModel 基地信息
     * @return
     */
    @PutMapping("/putFarm")
    public Result putFarm(@RequestBody FarmModel farmModel) {
        log.info("farmModel------putFarm--->{}", farmModel);
        Farm farm = new Farm();
        BeanUtil.copyProperties(farmModel, farm);
//        Province province = this.provinceService.getOptById(farmModel.getProvinceCode()).get();
        farm.setProvince(farmModel.getProvinceCode());
//        City city = this.cityService.getOptById(farmModel.getCityCode()).get();
        farm.setCity(farmModel.getCityCode());
//        County county = this.countyService.getOptById(farmModel.getCountyCode()).get();
        farm.setDistrict(farmModel.getCountyCode());

        log.info("copy之后的farm对象是------>{}", farm);
        farmService.updateById(farm);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除某个基地
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteFarm/{id}")
    public Result deleteFarm(@PathVariable(value = "id") Integer id) {
        farmService.removeById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
